package com.tianjishuju.cimiss.station.station.util;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;

public class BigEtlUtils {
	public static HashMap<String, Object> cimissDataInit() {
		HashMap<String, Object> allElementsMap = new HashMap<String, Object>();

	    allElementsMap.put("Station_Name", "");
		allElementsMap.put("Lat", 0.0d);
		allElementsMap.put("Lon", 0.0d);
		allElementsMap.put("Alti", 0.0d);
		allElementsMap.put("PRS_Sensor_Alti", 0.0d);
		allElementsMap.put("WIN_S_Sensor_Heigh", 0.0d);
		allElementsMap.put("Station_Type", "");
		allElementsMap.put("Station_levl", "");
		allElementsMap.put("Admin_Code_CHN", "");
		allElementsMap.put("PRS", 0.0d);
		allElementsMap.put("PRS_Sea", 0.0d);
		allElementsMap.put("PRS_Change_3h", 0.0d);
		allElementsMap.put("PRS_Max", 0.0d);
		allElementsMap.put("PRS_Max_OTime", "");
		allElementsMap.put("PRS_Min", 0.0d);
		allElementsMap.put("PRS_Min_OTime", "");
		allElementsMap.put("TEM", 0.0d);
		allElementsMap.put("TEM_Max", 0.0d);
		allElementsMap.put("TEM_Max_OTime", "");
		allElementsMap.put("TEM_Min", 0.0d);
		allElementsMap.put("TEM_Min_OTime", "");
		allElementsMap.put("TEM_ChANGE_24h", 0.0d);
		allElementsMap.put("TEM_Max_24h", 0.0d);
		allElementsMap.put("TEM_Min_24h", 0.0d);
		allElementsMap.put("DPT", 0.0d);
		allElementsMap.put("RHU", 0.0d);
		allElementsMap.put("RHU_Min", 0.0d);
		allElementsMap.put("RHU_Min_OTIME", "");
		allElementsMap.put("VAP", 0.0d);
		allElementsMap.put("PRE_1h", 0.0d);
		allElementsMap.put("PRE_3h", 0.0d);
		allElementsMap.put("PRE_6h", 0.0d);
		allElementsMap.put("PRE_12h", 0.0d);
		allElementsMap.put("PRE_24h", 0.0d);
		allElementsMap.put("PRE_Arti_Enc_CYC", "");
		allElementsMap.put("PRE", 0.0d);
		allElementsMap.put("EVP_Big", 0.0d);
		allElementsMap.put("LGST", 0.0d);
		allElementsMap.put("LGST_Max", 0.0d);
		allElementsMap.put("LGST_Max_OTime", "");
		allElementsMap.put("LGST_Min", 0.0d);
		allElementsMap.put("LGST_Min_OTime", "");
		allElementsMap.put("VIS_HOR_1MI", 0.0d);
		allElementsMap.put("VIS_HOR_10MI", 0.0d);
		allElementsMap.put("VIS_Min", 0.0d);
		allElementsMap.put("VIS_Min_OTime", "");
		allElementsMap.put("VIS", 0.0d);
		allElementsMap.put("WIN_D_Avg_2mi", 0.0d);
		allElementsMap.put("WIN_S_Avg_2mi", 0.0d);
		allElementsMap.put("WIN_D_Avg_10mi", 0.0d);
		allElementsMap.put("WIN_S_Avg_10mi", 0.0d);
		allElementsMap.put("WIN_D_S_Max", 0.0d);
		allElementsMap.put("WIN_S_Max", 0.0d);
		allElementsMap.put("WIN_S_Max_OTime", "");
		allElementsMap.put("WIN_D_INST", 0.0d);
		allElementsMap.put("WIN_S_INST", 0.0d);
		allElementsMap.put("WIN_D_INST_Max", 0.0d);
		allElementsMap.put("WIN_S_Inst_Max", 0.0d);
		allElementsMap.put("WIN_S_INST_Max_OTime", "");
		allElementsMap.put("WIN_D_Inst_Max_6h", 0.0d);
		allElementsMap.put("WIN_S_Inst_Max_6h", 0.0d);
		allElementsMap.put("WIN_D_Inst_Max_12h", 0.0d);
		allElementsMap.put("WIN_S_Inst_Max_12h", 0.0d);
		allElementsMap.put("GST", 0.0d);
		allElementsMap.put("GST_Max", 0.0d);
		allElementsMap.put("GST_Max_Otime", "");
		allElementsMap.put("GST_Min", 0.0d);
		allElementsMap.put("GST_Min_OTime", "");
		allElementsMap.put("GST_Min_12h", 0.0d);
		allElementsMap.put("GST_5cm", 0.0d);
		allElementsMap.put("GST_10cm", 0.0d);
		allElementsMap.put("GST_15cm", 0.0d);
		allElementsMap.put("GST_20cm", 0.0d);
		allElementsMap.put("GST_40cm", 0.0d);
		allElementsMap.put("GST_80cm", 0.0d);
		allElementsMap.put("GST_160cm", 0.0d);
		allElementsMap.put("GST_320cm", 0.0d);
		allElementsMap.put("CLO_Cov", 0.0d);
		allElementsMap.put("CLO_Cov_Low", 0.0d);
		allElementsMap.put("CLO_COV_LM", 0.0d);
		allElementsMap.put("CLO_Height_LoM", 0.0d);
		allElementsMap.put("CLO_FOME_1", 0.0d);
		allElementsMap.put("CLO_FOME_2", 0.0d);
		allElementsMap.put("CLO_FOME_3", 0.0d);
		allElementsMap.put("CLO_FOME_4", 0.0d);
		allElementsMap.put("CLO_FOME_5", 0.0d);
		allElementsMap.put("CLO_FOME_6", 0.0d);
		allElementsMap.put("CLO_FOME_7", 0.0d);
		allElementsMap.put("CLO_FOME_8", 0.0d);
		allElementsMap.put("CLO_Fome_Low", 0.0d);
		allElementsMap.put("CLO_FOME_MID", 0.0d);
		allElementsMap.put("CLO_Fome_High", 0.0d);
		allElementsMap.put("WEP_Now", 0.0d);
		allElementsMap.put("WEP_Past_CYC", "");
		allElementsMap.put("WEP_Past_1", 0.0d);
		allElementsMap.put("WEP_Past_2", 0.0d);
		allElementsMap.put("SCO", "");
		allElementsMap.put("Snow_Depth", 0.0d);
		allElementsMap.put("Snow_PRS", 0.0d);
		allElementsMap.put("FRS_1st_Top", 0.0d);
		allElementsMap.put("FRS_1st_Bot", 0.0d);
		allElementsMap.put("FRS_2nd_Top", 0.0d);
		allElementsMap.put("FRS_2nd_Bot", 0.0d);
		allElementsMap.put("station_time", "");
		allElementsMap.put("Station_ID_C", "");
		allElementsMap.put("pre_id", "");
		allElementsMap.put("Country", "");
		allElementsMap.put("Province", "");
		allElementsMap.put("City", "");
		allElementsMap.put("Cnty", "");
		allElementsMap.put("Town", "");

		return allElementsMap;
	}

	// map < -- > obj :https://www.cnblogs.com/XuYiHe/p/6871799.html
	/**
	 * A 使用org.apache.commons.beanutils进行转换
	 */

	/**
	 * B 使用Introspector进行转换
	 */

	public static Object mapToObjectB(Map<String, Object> map, Class<?> beanClass) throws Exception {
		if (map == null)
			return null;

		Object obj = beanClass.newInstance();

		BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
		PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
		for (PropertyDescriptor property : propertyDescriptors) {
			Method setter = property.getWriteMethod();
			if (setter != null) {
				setter.invoke(obj, map.get(property.getName()));
			}
		}

		return obj;
	}

	public static Map<String, Object> objectToMapB(Object obj) throws Exception {
		if (obj == null)
			return null;

		Map<String, Object> map = new HashMap<String, Object>();

		BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
		PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
		for (PropertyDescriptor property : propertyDescriptors) {
			String key = property.getName();
			if (key.compareToIgnoreCase("class") == 0) {
				continue;
			}
			Method getter = property.getReadMethod();
			Object value = getter != null ? getter.invoke(obj) : null;
			map.put(key, value);
		}

		return map;
	}

	/**
	 * C 使用reflect进行转换
	 */

	public static Object mapToObjectC(Map<String, Object> map, Class<?> beanClass) throws Exception {
		if (map == null)
			return null;

		Object obj = beanClass.newInstance();

		Field[] fields = obj.getClass().getDeclaredFields();
		for (Field field : fields) {
			int mod = field.getModifiers();
			if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
				continue;
			}

			field.setAccessible(true);
			field.set(obj, map.get(field.getName()));
		}

		return obj;
	}

	public static Map<String, Object> objectToMapC(Object obj) throws Exception {
		if (obj == null) {
			return null;
		}

		Map<String, Object> map = new HashMap<String, Object>();

		Field[] declaredFields = obj.getClass().getDeclaredFields();
		for (Field field : declaredFields) {
			field.setAccessible(true);
			map.put(field.getName(), field.get(obj));
		}

		return map;
	}
}
